package com.ccrfid.rmc6.service.business.prison.rollcall;

import com.ccrfid.rmc6.entity.business.prison.rollcall.PrisonRollcallResultDetailEntity;
import com.ccrfid.rmc6.entity.business.prison.rollcall.PrisonRollcallResultEntity;
import com.ccrfid.rmc6.entity.business.prison.rollcall.PrisonRollcallTaskTimeEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelView;
import com.ccrfid.rmc6.pojo.Constants;
import com.ccrfid.rmc6.pojo.dto.prison.rollcall.PrisonRollcallPoliceDto;
import com.ccrfid.rmc6.pojo.dto.prison.rollcall.PrisonRollcallResultDto;
import com.ccrfid.rmc6.repository.business.prison.rollcall.PrisonRollcallResultDetailRepository;
import com.ccrfid.rmc6.repository.business.prison.rollcall.PrisonRollcallResultRepository;
import com.ccrfid.rmc6.repository.business.prison.rollcall.PrisonRollcallTaskTimeRepository;
import com.ccrfid.rmc6.repository.personnel.PersonnelViewRepository;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.sys.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PrisonRollcallServiceImpl implements PrisonRollcallService {
    private static final Logger logger = LoggerFactory.getLogger(PrisonRollcallServiceImpl.class);
    @Autowired
    private PrisonRollcallTaskTimeRepository rollcallTaskTimeRepository;
    @Autowired
    private PrisonRollcallResultRepository rollcallResultRepository;
    @Autowired
    private PrisonRollcallResultDetailRepository rollcallResultDetailRepository;
    @Autowired
    private PersonnelViewRepository personnelViewRepository;
    @Autowired
    private DictService dictService;

    @Override
    public List<PrisonRollcallTaskTimeEntity> getRollcallTaskTimeList() {
        return rollcallTaskTimeRepository.getAllByOrderByTaskTimeAsc();
    }

    @Override
    public PrisonRollcallTaskTimeEntity createRollcallTaskTime(Time timeValue) {
        PrisonRollcallTaskTimeEntity taskTime = rollcallTaskTimeRepository.getByTaskTime(timeValue);
        if (taskTime == null) {
            PrisonRollcallTaskTimeEntity timeEntity = new PrisonRollcallTaskTimeEntity();
            timeEntity.setTaskTime(timeValue);
            return rollcallTaskTimeRepository.save(timeEntity);
        } else {
            return taskTime;
        }
    }

    @Override
    public Boolean deleteRollcallTaskTime(Time timeValue) {
        if (rollcallTaskTimeRepository.existsByTaskTime(timeValue)) {
            rollcallTaskTimeRepository.deleteAllByTaskTime(timeValue);
        }
        return true;
    }

    @Override
    public Page<PrisonRollcallResultEntity> getRollcallResultList(SearchCriteria criteria, Pageable pageable) {
        return rollcallResultRepository.search(criteria, pageable);
    }

    @Override
    public Page<PrisonRollcallResultDetailEntity> getRollcallDetailResult(SearchCriteria criteria, Pageable pageable) {
        return rollcallResultDetailRepository.search(criteria, pageable);
    }

    @Override
    public Boolean uploadRollcallResult(PrisonRollcallResultDto resultDto) {
        PrisonRollcallResultEntity rollcallResultEntity = resultDto.getInfo();
        List<PrisonRollcallResultDetailEntity> rollcallResultDetailEntityList = resultDto.getDetails();
        if (rollcallResultEntity == null || rollcallResultDetailEntityList == null) {
            return false;
        }
        PrisonRollcallResultEntity resultEntity =
                rollcallResultRepository.getByPoliceIdAndPlaceIdAndTaskTimeAndRollcallTime(
                        rollcallResultEntity.getPoliceId(), rollcallResultEntity.getPlaceId(),
                        rollcallResultEntity.getTaskTime(), rollcallResultEntity.getRollcallTime());
        //判断是否重复提交
        if (resultEntity != null) {
            resultEntity.setPoliceSn(rollcallResultEntity.getPoliceSn());
            resultEntity.setPoliceName(rollcallResultEntity.getPoliceName());
            resultEntity.setPlaceName(rollcallResultEntity.getPlaceName());
            resultEntity.setTotalCount(rollcallResultEntity.getTotalCount());
            resultEntity.setAttendanceCount(rollcallResultEntity.getAttendanceCount());
            resultEntity.setAttendanceRate(rollcallResultEntity.getAttendanceRate());
            resultEntity = rollcallResultRepository.save(resultEntity);
            for (PrisonRollcallResultDetailEntity detailEntity : rollcallResultDetailEntityList) {
                PrisonRollcallResultDetailEntity resultDetailEntity =
                        rollcallResultDetailRepository.getByRollcallResultIdAndPersonnelId(
                                resultEntity.getId(), detailEntity.getPersonnelId());
                if (resultDetailEntity != null) {
                    resultDetailEntity.setPersonnelSn(detailEntity.getPersonnelSn());
                    resultDetailEntity.setPersonnelName(detailEntity.getPersonnelName());
                    resultDetailEntity.setCardNumber(detailEntity.getCardNumber());
                    resultDetailEntity.setOrgId(detailEntity.getOrgId());
                    resultDetailEntity.setOrgName(detailEntity.getOrgName());
                    resultDetailEntity.setResultStatus(detailEntity.getResultStatus());
                    resultDetailEntity.setRemark(detailEntity.getRemark());
                    resultDetailEntity.setRollcallTime(detailEntity.getRollcallTime());
                    rollcallResultDetailRepository.save(resultDetailEntity);
                } else {
                    detailEntity.setRollcallResultId(resultEntity.getId());
                    rollcallResultDetailRepository.save(detailEntity);
                }
            }
        } else {
            rollcallResultEntity = rollcallResultRepository.save(rollcallResultEntity);
            for (PrisonRollcallResultDetailEntity detailEntity : rollcallResultDetailEntityList) {
                detailEntity.setRollcallResultId(rollcallResultEntity.getId());
                rollcallResultDetailRepository.save(detailEntity);
            }
        }
        return true;
    }

    @Override
    public List<PrisonRollcallPoliceDto> getRollcallPolicemanList() {
        List<Integer> typeList = dictService.getDictAndSubDictIdListById(Constants.PERSONNEL_TYPE.WORKER.id);
        List<PersonnelView> policemanList = personnelViewRepository.getAllByEnabledAndPersonnelTypeIn(1, typeList);
        if (policemanList != null && policemanList.size() > 0) {
            List<PrisonRollcallPoliceDto> dtoList = new ArrayList<>();
            for (PersonnelView policeman : policemanList) {
                PrisonRollcallPoliceDto dto = new PrisonRollcallPoliceDto(
                        policeman.getId(), policeman.getPersonnelSn(), policeman.getPersonnelName(),
                        policeman.getCardNumber(), policeman.getOrgId(), policeman.getOrgName());
                dtoList.add(dto);
            }
            return dtoList;
        } else {
            return new ArrayList<>();
        }
    }
}
